package com.tywj.buscustomerapp.presenter;

import android.content.Context;
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.tywj.buscustomerapp.Config;
import com.tywj.buscustomerapp.model.bean.BusTravelBean;
import com.tywj.buscustomerapp.model.bean.DaysNew;
import com.tywj.buscustomerapp.model.bean.LineSearchBean;
import com.tywj.buscustomerapp.model.bean.RequestBean;
import com.tywj.buscustomerapp.presenter.contract.newBuyTicketContract;
import com.tywj.buscustomerapp.view.myview.ToastJoe;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class newBuyTicketPresenter implements newBuyTicketContract.newBuyTicketPresenter{
    private String TAG = "newBuyTicketPresenter";
    private int ERROR_NUMBER = 0;
    private newBuyTicketContract.newBuyTicketView view;
    public newBuyTicketPresenter(newBuyTicketContract.newBuyTicketView view) {
        this.view = view;
    }

    @Override
    public void getDays(final String lineNum, final String phone) {
        if (view!=null) {
            RequestQueue requestQueue = Volley.newRequestQueue(view.getViewContext());
            String url = Config.APIURL + "calender/list";
            JSONObject object = new JSONObject();
            try {
                object.put("yhm", phone);
                object.put("xlbh", lineNum);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, object, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    ERROR_NUMBER = 0;
                    DaysNew data = new Gson().fromJson(String.valueOf(response), DaysNew.class);
                    Log.i(TAG, data.toString());
                    if (data.getReCode().equals("201")) {
                        if (data.getData() != null && data.getData().size() > 0) {
                            if (view != null) {
                                view.loadDays(data.getData());
                            }
                        } else {
                            if (view != null)
                                ToastJoe.getmToastJoe().ToastLongShow(view.getViewContext(), null, data.getCodeTxt());
                        }
                    } else {
                        if (view != null)
                            ToastJoe.getmToastJoe().ToastLongShow(view.getViewContext(), null, data.getCodeTxt());
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if (ERROR_NUMBER < 3) {
                        getDays(lineNum, phone);
                        ERROR_NUMBER++;
                    } else {
                        ERROR_NUMBER = 0;
                        if (view != null && view.getViewContext()!=null) {
                            ToastJoe.getmToastJoe().ToastLongShow(view.getViewContext(), null, "网络错误，请重试");
                            view.finishView();
                        }
                    }
                }
            });
            requestQueue.add(jsonObjectRequest);
        }
    }

    @Override
    public void doBuyTicketOnOnce(Context context, String yhm, String ch, String gmqsz, String gmzdz, List<String> ccsjs, String scsj, String xcsj, int number) {
        if (view!=null)
            view.showLoading();
        doBuyOnceTicket(context,yhm,ch,gmqsz,gmzdz,ccsjs,scsj,xcsj,number);
    }

    @Override
    public void getLineMessage(final String lineNumber) {
        if (view!=null) {
            ERROR_NUMBER = 0;
            RequestQueue requestQueue = Volley.newRequestQueue(view.getViewContext());
            String url = Config.APIURL + "line/getStation";
            JSONObject object = new JSONObject();
            try {
                object.put("xlbh", lineNumber);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, object, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.i("newBUYTICKET",String.valueOf(response));
                    LineSearchBean data = new Gson().fromJson(String.valueOf(response), LineSearchBean.class);
                    if (view!=null){
                        if (data.getReCode().equals("201")) {
                            if (data.getData() != null) {
                                BusTravelBean.DataBean.ResultListBean bean = data.getData();
                                view.loadeLineMessage(bean);
                            } else {
                                ToastJoe.getmToastJoe().ToastLongShow(view.getViewContext(), null, data.getCodeTxt());
                            }
                        } else {
                            ToastJoe.getmToastJoe().ToastLongShow(view.getViewContext(), null, data.getCodeTxt());
                        }
                    }

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if (ERROR_NUMBER < 3) {
                        getLineMessage(lineNumber);
                        ERROR_NUMBER++;
                    } else {
                        ERROR_NUMBER = 0;
                        if (view!=null) {
                            ToastJoe.getmToastJoe().ToastLongShow(view.getViewContext(), null, "网络错误，请重试");
                            view.finishView();
                        }
                    }
                }
            });
            requestQueue.add(jsonObjectRequest);
        }
    }

    @Override
    public void doBuyTicketMonth(Context context, String yhm, String ch, String gmqsz, String gmzdz) {
        if (view!=null)
            view.showLoading();
        doBuyMonthTicket(context,yhm,ch,gmqsz,gmzdz);
    }


    @Override
    public void onDestroy() {
        view = null;
        System.gc();
    }

    public RequestBean doBuyOnceTicket(Context context, String yhm, String ch, String gmqsz, String gmzdz, List<String> ccsjs,
                                       String scsj, String xcsj, int number){
        if (view!=null) {
            //创建一个请求队列
            RequestQueue requestQueue = Volley.newRequestQueue(context);
            //创建一个请求
            String url = Config.SBURL + "/order/chepiaoinsert";
            JSONObject object = new JSONObject();
            try {
                object.put("yhm", yhm);
                object.put("ch", ch);
                object.put("gmqszId", gmqsz);
                object.put("gmzdzId", gmzdz);
//            object.put("ccsj",ccsj);
                JSONArray jsonArray = new JSONArray();
                for (int i = 0; i < ccsjs.size(); i++) {
                    jsonArray.put(ccsjs.get(i));
                }
                object.put("ccsjs", jsonArray);
                object.put("scsj", scsj);
                object.put("xcsj", xcsj);
                JSONArray jsonArray1 = new JSONArray();
                for (int i = 0; i < ccsjs.size(); i++) {
                    jsonArray1.put(number);
                }
                object.put("nums", jsonArray1);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, object, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    Log.i("taylor", String.valueOf(jsonObject));
                    RequestBean data = new Gson().fromJson(String.valueOf(jsonObject), RequestBean.class);
                    if (data != null) {
                        view.loadeIsBuySuccess(data);
                    } else {
                        view.loadError("购买失败");
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    view.loadError("购买失败");
                }
            });
            //将创建的请求添加到队列中
            requestQueue.add(jsonObjectRequest);
            return null;
        }else{
            return null;
        }
    }

    public RequestBean doBuyMonthTicket(Context context, String yhm, String ch, String gmqsz, String gmzdz){
        if (view!=null) {
            //创建一个请求队列
            RequestQueue requestQueue = Volley.newRequestQueue(context);
            //创建一个请求
            String url = Config.SBURL + "/order/addMonthTicket";
            JSONObject object = new JSONObject();
            try {
                object.put("yhm", yhm);
                object.put("ch", ch);
                object.put("gmqszId", gmqsz);
                object.put("gmzdzId", gmzdz);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, object, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    Log.i("taylor", String.valueOf(jsonObject));
                    RequestBean data = new Gson().fromJson(String.valueOf(jsonObject), RequestBean.class);
                    if (data != null) {
                        view.loadeIsBuySuccess(data);
                    } else {
                        view.loadError("购买失败");
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    view.loadError("购买失败");
                }
            });
            //将创建的请求添加到队列中
            requestQueue.add(jsonObjectRequest);
            return null;
        }else{
            return null;
        }
    }
}
